package util;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import pojo.Hoadon;
import pojo.Loaisanpham;
import pojo.Nhasanxuat;
import pojo.Quangcao;
import pojo.Sanpham;
import pojo.Taikhoan;
import dao.ChitiethoadonDAO;
import dao.HoadonDAO;
import dao.LoaisanphamDAO;
import dao.NhasanxuatDAO;
import dao.QuangcaoDAO;
import dao.SanphamDAO;
import dao.TaikhoanDAO;

public class SearchVisitor implements IVisitor {

	private List<Loaisanpham> resultLoaiSanPham = new ArrayList<Loaisanpham>();

	@Override
	public void accept(LoaisanphamDAO lspdao, String searchKey) {

		String hql = "select lsp from Loaisanpham lsp "
				+ "where lower(lsp.tenloaisanpham) like :searchKey";

		// ---------------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		// ---------------------------

		setResultLoaiSanPham(selectClause.list());
		System.out.println("Result size: " + getResultLoaiSanPham().size());
		session.close();
	}

	public List<Loaisanpham> getResultLoaiSanPham() {
		return resultLoaiSanPham;
	}

	public void setResultLoaiSanPham(List<Loaisanpham> resultLoaiSanPham) {
		this.resultLoaiSanPham = resultLoaiSanPham;
	}

	// ------------------------------------------
	private List<Sanpham> resultSanPham = new ArrayList<Sanpham>();

	@Override
	public void accept(SanphamDAO spdao, String searchKey, Integer from, Integer max) {

		String hql = "select sp from Sanpham sp "
				+ "where lower(sp.tensanpham) like :searchKey";

		// ---------------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		// ---------------------------
		if(from != null)
		{
			selectClause.setFirstResult(from);
			selectClause.setMaxResults(max);
		}
		setResultSanPham(selectClause.list());
		System.out.println("Result size: " + getResultSanPham().size());
		session.close();
	}

	public List<Sanpham> getResultSanPham() {
		return resultSanPham;
	}

	public void setResultSanPham(List<Sanpham> resultSanPham) {
		this.resultSanPham = resultSanPham;
	}

	// ------------------------------------------
	private List<Nhasanxuat> resultNhaSanXuat = new ArrayList<Nhasanxuat>();

	@Override
	public void accept(NhasanxuatDAO spdao, String searchKey) {

		String hql = "select nsx from Nhasanxuat nsx "
				+ "where lower(nsx.tennhansanxuat) like :searchKey";

		// ---------------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		// ---------------------------

		setResultNhaSanXuat(selectClause.list());
		System.out.println("Result size: " + getResultNhaSanXuat().size());
		session.close();
	}

	public List<Nhasanxuat> getResultNhaSanXuat() {
		return resultNhaSanXuat;
	}

	public void setResultNhaSanXuat(List<Nhasanxuat> resultNhaSanXuat) {
		this.resultNhaSanXuat = resultNhaSanXuat;
	}

	// ------------------------------------------Chưa sử dụng
	private List<Hoadon> resultHoaDon = new ArrayList<Hoadon>();

	@Override
	public void accept(ChitiethoadonDAO cthddao, String searchKey) {

		String hql = "select cthd.hoadon from Chitiethoadon cthd "
				+ " where lower(cthd.sanpham.tensanpham) like :searchKey";

		// --------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		// --------------------
		setResultHoaDon(selectClause.list());
		HoadonDAO hddao = new HoadonDAO();
		setResultHoaDon(hddao.removeDuplicate(getResultHoaDon()));

		System.out.println("Result size: " + getResultHoaDon().size());
		session.close();
	}

	public List<Hoadon> getResultHoaDon() {
		return resultHoaDon;
	}

	public void setResultHoaDon(List<Hoadon> resultHoaDon) {
		this.resultHoaDon = resultHoaDon;
	}

	// ------------------------------------------
	private List<Taikhoan> resultTaiKhoan = new ArrayList<Taikhoan>();

	@Override
	public void accept(TaikhoanDAO tkdao, String searchKey) {

		/*
		 * String hql = "select tk from Taikhoan tk " +
		 * "where lower(tk.tendangnhap) like '%" + searchKey.toLowerCase() +
		 * "%'" + " or lower(tk.hoten) like '%" + searchKey.toLowerCase() +
		 * "%'";
		 */
		String hql = "select tk from Taikhoan tk "
				+ " where lower(tk.tendangnhap) like :searchKey"
				+ " or lower(tk.hoten) like :searchKey";

		//------------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		//------------------------
		
		setResultTaiKhoan(selectClause.list());
		System.out.println("Result size: " + getResultTaiKhoan().size());

		session.close();
	}

	public List<Taikhoan> getResultTaiKhoan() {
		return resultTaiKhoan;
	}

	public void setResultTaiKhoan(List<Taikhoan> resultTaiKhoan) {
		this.resultTaiKhoan = resultTaiKhoan;
	}

	// ------------------------------------------
	private List<Quangcao> resultQuangCao = new ArrayList<Quangcao>();

	@Override
	public void accept(QuangcaoDAO qcdao, String searchKey) {

		String hql = "select qc from Quangcao qc "
				+ " where lower(qc.congty) like :searchKey"
				+ " or lower(qc.link) like :searchKey";

		//------------------------
		System.out.println("Search: " + hql);
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query selectClause = session.createQuery(hql);
		selectClause.setParameter("searchKey", "%" + searchKey.toLowerCase()
				+ "%");
		//------------------------
		setResultQuangCao(selectClause.list());
		System.out.println("Result size: " + getResultQuangCao().size());

		session.close();
	}

	public List<Quangcao> getResultQuangCao() {
		return resultQuangCao;
	}

	public void setResultQuangCao(List<Quangcao> resultQuangCao) {
		this.resultQuangCao = resultQuangCao;
	}

	

}
